home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 5_5.lha / 5_5 / intset.c < prev    next >
Text File  |  1993-08-08  |  876b  |  53 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. xtern int error(const char * ...);
  6. ifdef NONLOCAL    /* DELETE */
  7. include <intset.h>
  8. else /* DELETE */
  9. include "intset.h" /* DELETE */
  10. endif /* DELETE */
  11.  
  12. ntset::intset(int m, int n)
  13.  
  14.    if (m < 1 || n < m) error("illegal intset size");
  15.    cursize = 0;
  16.    maxsize = m;
  17.    x = new int[maxsize];
  18.  
  19.  
  20. ntset:: ~intset()
  21.  
  22.  
  23.    delete x;
  24.  
  25.  
  26. oid intset::insert(int t)
  27.  
  28.    if (++cursize > maxsize) error("too many elements in intset");
  29.    int i = cursize - 1;
  30.    x[i] = t;
  31.    while (i > 0 && x[i-1] > x[i])
  32. {
  33. int t = x[i];
  34. x[i] = x[i-1];
  35. x[i-1] = t;
  36. i--;
  37. }
  38.  
  39.  
  40. nt intset::member(int t)
  41.  
  42.    int l = 0;
  43.    int u = cursize - 1;
  44.    while (l <= u)
  45. {
  46. int m = (l+u) / 2;
  47. if (t < x[m]) u = m - 1;
  48. else if (t > x[m]) l = m + 1;
  49. else return 1;
  50. }
  51.    return 0;
  52.  
  53.